package com.igeek.dao;

import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 数据库交互层：
 *      转账金额的转出和转入
 */
public class AccountDao {

    /**
     * 金额的转出
     *
     * @param conn 获取连接
     * @param money 转账的金额
     * @param from 要转出的账户
     * @return
     */
    public int accountOut(Connection conn,double money,String from) throws SQLException {
        QueryRunner runner = new QueryRunner();

        String sql = "update t_account set balance = balance - ? where id =  ?";

        //返回更新的行数
        return  runner.update(conn,sql,money,from);

    }

    /**
     * 金额的转入
     * @param conn
     * @param money
     * @param to
     * @return
     */
    public int accountInt(Connection conn,double money,String to) throws SQLException {

        QueryRunner runner = new QueryRunner();

        String sql = "update t_account set balance = balance + ? where id = ?";

        return runner.update(conn,sql,money,to);
    }
}
